#include "BoardTile.h"

namespace Dicewars
{
	BoardTile::BoardTile(Vector2* tilePosition)
	{
		Box boundingBox = Box();
		boundingBox.SetScale(2.0 , 0.4, 2.0);
		model = new ColladaModel("data/tile.dae", boundingBox);
		position = tilePosition;
		//explosion = new ParticleSystem();
		model->SetSpecular(0.5, 0.5, 0.5, 1.0);
		model->SetShininess(64.0);
		owner = NULL;
	}
	
	BoardTile::~BoardTile()
	{
		delete model;
		delete position;
	}
	
	void BoardTile::Draw()
	{
		model->Draw();
	}
	
	bool BoardTile::Intersect(PickRay* r, float t0, float t1)
	{
		return model->GetBoundingBox().Intersect(r, t0, t1);
	}
	
	Die* BoardTile::PushDie(Die* newDie)
	{
		dice.push_back(newDie);
		return newDie;
	}
	
	Die* BoardTile::PopDie()
	{
		Die* tmp = dice.back();
		dice.pop_back();
		return tmp;
	}
	
	void BoardTile::PlaceDie(Die* newDie)
	{
		float diePositionX = GetDiceCount()%2 != 0 ? GetModel()->GetTranslation()->x -0.5 : GetModel()->GetTranslation()->x + 0.5;
		float diePositionY = GetModel()->GetTranslation()->y + 0.40 + ((GetDiceCount()/2) * 0.5f);
		float diePositionZ = GetModel()->GetTranslation()->z;
		newDie->GetModel()->SetTranslation(diePositionX, diePositionY, diePositionZ);
		newDie->GetModel()->SetScale(0.25, 0.25, 0.25);
	}
	
	const Vector2* BoardTile::GetPosition() const
	{
		return position;
	}
	
	ColladaModel* BoardTile::GetModel() const
	{
		return model;
	}

	unsigned int BoardTile::GetDiceCount()
	{
		return dice.size();
	}
	
	Player* BoardTile::GetOwner() const
	{
		return owner;
	}
	
	//HACK class Player is forwared declared - we cannot obtain color from newOwner
	void BoardTile::SetOwner(Player* newOwner, GLfloat* color) 
	{
		owner = newOwner;
		model->SetAmbient(color[0], color[1], color[2], color[3]);
	}
}